#include "StdAfx.h"
#include "CairoLibrary.h"
#include "cairo/cairo-win32.h"
#include "cairo/cairo.h"

CairoLibrary::CairoLibrary(HWND winHandle)
{
	HDC dc = GetDC(winHandle);
	cairo_surface_t *surface = cairo_win32_surface_create(dc);
	context = cairo_create(surface);
	
}

CairoLibrary::CairoLibrary(const CairoLibrary& copy)
{
	cairo_surface_t* surface = cairo_get_target(copy.context);
	context = cairo_create(surface);
}

CairoLibrary::~CairoLibrary(void)
{
	if (context!=NULL)
		delete context;
}

void CairoLibrary::DrawLine(int x1, int y1, int x2, int y2, COLORREF color, int strokewidth)
{
	cairo_set_source_rgb(context,GetRValue(color),GetGValue(color),GetBValue(color));
	cairo_set_line_width(context, strokewidth);
	cairo_move_to(context, x1, y1);
	cairo_line_to(context, x2,  y2);
	cairo_stroke(context);
	write_to_png(
}

void CairoLibrary::DrawRectangle(int x, int y, int width, int height, COLORREF color, int strokewidth, COLORREF fill)
{
	if(strokewidth == 1)
		strokewidth++;
	cairo_set_source_rgb(context,GetRValue(color),GetGValue(color),GetBValue(color));
	cairo_set_line_width(context, strokewidth);
	cairo_rectangle(context, x, y, width, height);
	cairo_stroke_preserve(context);
	cairo_set_source_rgb(context,GetRValue(fill),GetGValue(fill),GetBValue(fill));
	cairo_fill(context);
}

void CairoLibrary::DrawEllipse(int cx, int cy, int rx, int ry, COLORREF color, int strokewidth, COLORREF fill)
{
	cairo_set_source_rgb(context,GetRValue(color),GetGValue(color),GetBValue(color));
	cairo_set_line_width(context, strokewidth);
	cairo_scale(context,1,(float)ry/rx);
	cairo_arc(context,cx, cy*(float)rx/ry,rx,0,3.14*2);
	cairo_scale(context,1,1);
	cairo_stroke_preserve(context);

	cairo_set_source_rgb(context,GetRValue(fill),GetGValue(fill),GetBValue(fill));
	cairo_fill(context);
}

IGraphicLibrary* CairoLibrary::Clone()
{
	return new CairoLibrary(*this);
}
